package com.itheima;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class testGenJwt {


    @Test
    public void testGenJwt(){
        Map<String,Object> claims= new HashMap<>();
        claims.put("id",10);
        claims.put("login-User","junming");

        // 生成令牌
        String jwt= Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,"shuinjihi")
                .addClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis()+60*1000))  //12*60*60*1000
                .compact();
        System.out.println(jwt);
    }



    // 命令牌解析测试
    @Test
    public void testParseJwt(){
        Claims claims= Jwts.parser().setSigningKey("shuinjihi")
                .parseClaimsJws("eyJhbGciOiJIUzI1NiJ9.eyJsb2dpbi1Vc2VyIjoianVubWluZyIsImlkIjoxMCwiZXhwIjoxNzUxNDU5OTU1fQ.6aE3pIFnCdizVLN9KSMgGXebpkEm142nwn9fbEpF6MM")
                .getBody();
        System.out.println(claims);
    }

}
